package wiimote.MoteJ;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import wiimote.WiimoteJ.OmniController;

import motej.*;
import motej.event.*;
import motej.request.ReportModeRequest;
import motejx.extensions.nunchuk.*;

@SuppressWarnings("serial")
public class NunchukGuiMoteJ implements 	
	MoteFinderListener, 
	ExtensionListener, 
	AnalogStickListener, 
	NunchukButtonListener{

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new NunchukGuiMoteJ();
			}
		});
	}

	//Wiimote comm
	private Mote mote;
	private MoteFinder finder;
	private Nunchuk nunchuk;

	//omni
	private OmniController omniController;
	private final int minPower = -100, maxPower = 100;
	private final String nxtName = "LejosNxt";

	//GUI
	private JFrame frame;
	private JLabel moteLabel = new JLabel("none"), extensionLabel = new JLabel("none");
	private JLabel nunchuckX = new JLabel("x:"), nunchuckY = new JLabel("y:");
	private JButton zButton = new JButton("Z"), cButton = new JButton("C");
	private Color originalColor;
	public JSlider MotorA, MotorB, MotorC;

	private Action startDiscoverAction = new AbstractAction() {
		public void actionPerformed(ActionEvent e) {
			finder.startDiscovery();
		}
	};
	
	private Action startNxtConnect = new AbstractAction() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			omniController.openConnection("LejosNxt");
		}
	};

	private Action cancelDiscoverAction = new AbstractAction() {
		public void actionPerformed(ActionEvent e) {
			finder.stopDiscovery();
		}
	};

	public NunchukGuiMoteJ() {
		finder = MoteFinder.getMoteFinder();
		finder.addMoteFinderListener(this);

//		omniController = new OmniController(this);

		initGui();
	}

	public void analogStickChanged(AnalogStickEvent evt) {
		nunchuckX.setText("x:" + evt.getPoint().x);
		nunchuckY.setText("y:" + evt.getPoint().y);

//		omniController.analogStickChanged(evt.getPoint());

		frame.pack();
	}

	public void buttonPressed(NunchukButtonEvent evt) {
		if (evt.isButtonCPressed()) {
			cButton.setBackground(Color.BLUE);
		} else {
			cButton.setBackground(originalColor);
		}

		if (evt.isButtonZPressed()) {
			zButton.setBackground(Color.BLUE);
		} else {
			zButton.setBackground(originalColor);
		}
	}

	public void extensionConnected(ExtensionEvent evt) {
		final Extension ext = evt.getExtension();

		if (ext instanceof Nunchuk) {
			setNunchuckExtension((Nunchuk)ext);
		}
	}

	public void extensionDisconnected(ExtensionEvent evt) {
		System.out.println("Extension disconnected");
	}

	private void setNunchuckExtension(Nunchuk ext){
		nunchuk = ext;
		nunchuk.addAnalogStickListener(this);
		nunchuk.addNunchukButtonListener(this);

		mote.setReportMode(ReportModeRequest.DATA_REPORT_0x32);
		frame.pack();
	}

	public void moteFound(final Mote mote) {
		this.mote = mote;
		mote.setPlayerLeds(new boolean[] {true, false, false, false} );

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				moteLabel.setText(mote.toString());
			}
		});

		mote.addExtensionListener(this);

		//add nunchuck if it is already connected
		if(mote.getExtension() != null && mote.getExtension() instanceof Nunchuk){
			setNunchuckExtension((Nunchuk) mote.getExtension());
		}
		frame.pack();
	}

	protected void initGui() {
		frame = new JFrame("Nunchuk Gui");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				if (mote != null) {
					mote.setReportMode(ReportModeRequest.DATA_REPORT_0x30);
					mote.disconnect();
				}
			}
		});

		JButton discoverButton = new JButton(startDiscoverAction);
		discoverButton.setText("Start Discovery");
		JButton cancelDiscoverButton = new JButton(cancelDiscoverAction);
		cancelDiscoverButton.setText("Cancel Discovery");
		JButton connectToNxt = new JButton(startNxtConnect);
		cancelDiscoverButton.setText("Connect to " + nxtName);

		cButton.setEnabled(false);
		zButton.setEnabled(false);

		Container pane = frame.getContentPane();

		//sliders
		MotorA = new JSlider(JSlider.VERTICAL, minPower, maxPower, 0);
		MotorB = new JSlider(JSlider.VERTICAL, minPower, maxPower, 0);
		MotorC = new JSlider(JSlider.VERTICAL, minPower, maxPower, 0);
		MotorA.setEnabled(false);
		MotorB.setEnabled(false);
		MotorC.setEnabled(false);

		pane.setLayout(new FlowLayout());
		pane.add(discoverButton);
		pane.add(cancelDiscoverButton);
		pane.add(connectToNxt);
		pane.add(moteLabel);
		pane.add(extensionLabel);
		pane.add(cButton);
		pane.add(zButton);
		pane.add(nunchuckX);
		pane.add(nunchuckY);

		//Omni
		pane.add(MotorA);
		pane.add(MotorB);
		pane.add(MotorC);

		frame.pack();
		frame.setVisible(true);
		originalColor = zButton.getBackground();
	}
}
